package cn.com.open.itembank.service.impl;

import cn.com.open.itembank.common.GlobalRedisKey;
import cn.com.open.itembank.dto.ItemBankCount;
import cn.com.open.itembank.entity.CourseItemBank;
import cn.com.open.itembank.mapper.AppMapper;
import cn.com.open.itembank.redis.RedisConfig;
import cn.com.open.itembank.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("appService")
public class AppServiceImpl implements AppService {
    @Autowired
    private RedisConfig redis;

    @Autowired
    private AppMapper mapper;

    /**
     * 获取appKey列表
     *
     * @return appKey列表 app list
     */
    @Override
    public List<String> getAppList() {
        String key = GlobalRedisKey.Redis_Applist;
        if (redis.isExists(key)) {
            return redis.getObject(key, List.class);
        }

        List<String> result = mapper.getAppList();
        redis.set(key, result, GlobalRedisKey.Redis_Expire);
        return result;
    }

    /**
     * 根据appKey和orgid获取课程题库信息
     *
     * @param appKey 应用ID
     * @param orgId  机构ID
     * @return 可用题库列表 enabled by app id org id
     */
    @Override
    public List<CourseItemBank> getItembankListByAppKeyAndOrgId(String appKey, String orgId) {
        return mapper.getItembankListByAppKeyAndOrgId(appKey, orgId);
    }

    /**
     * 根据appKey获取对应题库题量列表
     *
     * @param appKey 应用ID
     * @return 对应题库题量列表
     */
    @Override
    public List<ItemBankCount> getItembankQuestionCountByAppKey(String appKey) {
        return mapper.getItemBankQuestionCountByAppKey(appKey);
    }
}
